import { _decorator, Node, instantiate, Label, tween, Vec2, UIOpacity, UITransform, v3, v2, EventTouch, Vec3, Prefab, sys, Component, Tween, random, randomRange, director, } from 'cc';
import UI from 'db://assets/Script/Manager/UI';
import { DaoShuiPaiXu_GameStorage } from './Data/DaoShuiPaiXu_GameStorage';
import DaoShuiPaiXu_GamePanel from './UI/DaoShuiPaiXu_GamePanel';
import { DaoShuiPaiXu_GameData } from './Data/DaoShuiPaiXu_GameData';
import DaoShuiPaiXu_WinPanel from './UI/DaoShuiPaiXu_WinPanel';
import { CupMgr } from './views/cupMgr';
import { events } from './enum/Enums';
import { DaoShuiPaiXu_Timer } from './DaoShuiPaiXu_Timer';
import DaoShuiPaiXu_LosePanel from './UI/DaoShuiPaiXu_LosePanel';

const { ccclass, property } = _decorator;

@ccclass
export default class DaoShuiPaiXu_GameManager extends Component {
    public static Instance: DaoShuiPaiXu_GameManager;

    @property(DaoShuiPaiXu_Timer)
    timer: DaoShuiPaiXu_Timer | null = null;

    async onLoad() {
        DaoShuiPaiXu_GameManager.Instance = this;

        DaoShuiPaiXu_GameStorage.initData();
        DaoShuiPaiXu_GameData.InitData();

        if (DaoShuiPaiXu_GameData.Game_mode == `mode_2`) {
            this.timer.node.active = true;

            if (this.timer) {
                this.timer.startCountdown();
                this.timer.node.on("countdown_end", () => {
                    this.onCountdownEnd();
                }, this);
            }
        }
        else {
            this.timer.node.active = false;
        }
    }

    onCountdownEnd() {
        console.log("倒计时结束------接收");

        UI.Instance.CreatePanel_sub(`DaoShuiPaiXu_Prefab`, `UI/DaoShuiPaiXu_LosePanel`, DaoShuiPaiXu_LosePanel);
    }

    Replay() {
        let currentSceneName = director.getScene().name;
        director.loadScene(currentSceneName);
    }

    Reborn() {
        DaoShuiPaiXu_LosePanel.Instance.ShowOff();

        this.timer.resetCountdown();
        this.timer.startCountdown();

    }

    onFinishOneLevel() {
        console.log(`onFinishOneLevel---------`)

        if (DaoShuiPaiXu_GameData.是否结束) {
            return;
        }

        DaoShuiPaiXu_GameData.是否结束 = true;
        this.timer.pauseCountdown();

        UI.Instance.CreatePanel_sub(`DaoShuiPaiXu_Prefab`, `UI/DaoShuiPaiXu_WinPanel`, DaoShuiPaiXu_WinPanel);
    }

    start() {
        DaoShuiPaiXu_GamePanel.Instance.ShowOn();

        CupMgr.Instance.node.on("level_finish", this.onFinishOneLevel, this);
    }

    // gameOver() {
    //     setTimeout(() => {
    //         UI.Instance.CreatePanel_sub(`JiuJiuXiaoDongWu_Prefab`, `UI/DaoShuiPaiXu_WinPanel`, DaoShuiPaiXu_WinPanel);
    //     }, 300);
    // }

    撤回() {
        if (!CupMgr.ins.checkCanUndo()) return;
        // if (Global.getPropsSetting("back") > 0) {
        //   Global.addPropsSetting("back", -1);
        //   director.emit(events.Back);
        // }
        // else {
        // console.log(e.target);
        director.emit(events.Back);
        // }
    }

    加瓶子() {
        // this.Button_撤回.on(Node.EventType.TOUCH_END, this.On_Button_撤回, this);

        // if (this.onceAdd) {
        // console.log('只能添加一个瓶子');
        //   director.emit(events.Toast, `每关只能添加一个瓶子`)
        //   return
        // }

        if (!CupMgr.ins.checkCanAddTube()) return;

        // if (Global.getPropsSetting("tube") > 0) {
        //   Global.addPropsSetting("tube", -1);
        //   this.onceAdd = true
        //   director.emit(events.AddTube);
        // }
        //  else {
        // console.log(e.target);
        // this.onceAdd = true
        director.emit(events.AddTube);
        // }
    }

    上一关() {
        DaoShuiPaiXu_GameData.Set_level(DaoShuiPaiXu_GameData.Get_level() - 1);

        CupMgr.Instance.nextLevel()
    }

    下一关() {
        DaoShuiPaiXu_GameData.Set_level(DaoShuiPaiXu_GameData.Get_level() + 1);

        let currentSceneName = director.getScene().name;
        director.loadScene(currentSceneName);

        // CupMgr.Instance.nextLevel()
    }
}

window['DaoShuiPaiXu_GameManager'] = DaoShuiPaiXu_GameManager;